﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using sydfdl.TMS.Model;
using sydfdl.TMS.Tool;

namespace sydfdl.TMS.BLL
{
    public class BLLT_C_Gift
    {

        public T_Base<T_C_Gift> GetList(string xml)
        {
            T_Base<T_C_Gift> list = new T_Base<T_C_Gift>();
            SqlParameter[] para = { 
                                  new SqlParameter("@para",SqlDbType.Xml,-1)
                                  };
            para[0].Value = xml;
            #region 获取数据
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.tmsConn, CommandType.StoredProcedure, @"P_B_GetGit", para))
            {
                List<T_C_Gift> inner = new List<T_C_Gift>();
                if (dr.Read() && dr.HasRows)
                    list.total = dr.GetInt32(0);
                if (dr.NextResult())
                {
                    while (dr.Read() && dr.HasRows)
                    {
                        T_C_Gift dto = new T_C_Gift();
                        dto.GID = dr.GetGuid(0);
                        dto.ProductCode = dr.GetString(1);
                        dto.ProductName = dr.GetString(2);
                        dto.Remark = dr.GetString(3);
                        dto.ScoreValue = dr.GetDouble(4);
                        dto.StartDate = dr.GetDateTime(5);
                        dto.MaxQty = dr.GetDouble(6);
                        dto.EndDate = dr.GetDateTime(7);
                        dto.CreateBy = dr.GetValue(8) is DBNull ? null : dr.GetString(8);
                        dto.CreateDate = dr.GetDateTime(9);
                        dto.CreateLastTime = dr.GetDateTime(10);
                        dto.CreateLastBy = dr.GetValue(11) is DBNull ? null : dr.GetString(11);
                        dto.ImgName = dr.GetValue(12) is DBNull ? null : dr.GetString(12);
                        inner.Add(dto);
                    }
                    list.data = inner;
                }
            }
            #endregion
            return list;
        }

        /// <summary>
        /// 操作数据
        /// </summary>
        /// <param name="xml">第一个为添加 ；第二个为修改；第三个为删除；</param>
        /// <returns></returns>
        public string Save(params IEnumerable<T_C_Gift>[] xml)
        {
            string addXmL = EFHelp<T_C_Gift>.GetXML(xml[0]);
            string editXML = EFHelp<T_C_Gift>.GetXML(xml[1]);
            string delXML = EFHelp<T_C_Gift>.GetXML(xml[2]);
            SqlParameter[] para = { 
                                  new SqlParameter("@addXmL",SqlDbType.Xml,-1),
                                  new SqlParameter("@editXML",SqlDbType.Xml,-1),
                                  new SqlParameter("@delXML",SqlDbType.Xml,-1),
                                  new SqlParameter("@userXML",SqlDbType.Xml,-1),
                                  new SqlParameter("@ReturnValue",SqlDbType.VarChar,100)
                                  };
            para[0].Value = addXmL;
            para[1].Value = editXML;
            para[2].Value = delXML;
            para[3].Value = null;
            para[4].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(SqlHelper.tmsConn, CommandType.StoredProcedure, @"P_B_SaveGift", para);
            return (para[4].Value ?? string.Empty).ToString();
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="_i">图片流</param>
        /// <param name="GID">ID</param>
        public string UpDataImage(byte[] _i, string fileName, Guid GID, string usercode = "")
        {
            SqlParameter[] para = { 
                                  new SqlParameter("@gimage",SqlDbType.VarBinary,-1),
                                  new SqlParameter("@ImgName",SqlDbType.VarChar,50),
                                  new SqlParameter("@GID",SqlDbType.UniqueIdentifier,-1),
                                  new SqlParameter("@usercode",SqlDbType.VarChar,100),
                                  new SqlParameter("@ReturnValue",SqlDbType.VarChar,100)
                                  };
            para[0].Value = _i;
            para[1].Value = fileName;
            para[2].Value = GID;
            para[3].Value = usercode;
            para[4].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(SqlHelper.tmsConn, CommandType.StoredProcedure, @"P_B_SaveGift_Image", para);
            return (para[4].Value ?? string.Empty).ToString();
        }


        public T_C_Gift GetImage(Guid gid)
        {
            SqlParameter[] para = { 
                                  new SqlParameter("@GID",SqlDbType.UniqueIdentifier,-1),
                                  };
            para[0].Value = gid;
            T_C_Gift dto = new T_C_Gift();
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.tmsConn, CommandType.StoredProcedure, @"P_B_GetGit_Image", para))
            {
                if (dr.Read() && dr.HasRows)
                {
                    dto.ProdImg = dr.GetValue(0) is DBNull ? null : dr.GetSqlBinary(0).Value;
                    dto.ImgName = dr.GetValue(1) is DBNull ? null : dr.GetString(1);
                }
            }
            return dto;
        }
    }
}
